﻿
using System;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;
using System.Data;

namespace Besture.UI.Admin.Room
{
    public partial class RoomFacilitiesSet : BasePage
    {
        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            //编辑编号
            InitData(ID);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if (id == 0) return;

            var dac = new DictionaryTypeDac();
            var model = dac.GetModel(id);
            if (model == null) return;
            txtCategoryName.Text = model.Name;
            txtSortID.SortValue = model.SortID != 0 ? model.SortID : 0;

        }
 
        public static string GetSortID()
        { 
            string sortID = "1";
            try
            {
                string sql = string.Format(@"SELECT TOP 1 (Code+1) AS Code FROM TBDictionaryType where Type=8 ORDER BY Code DESC");
                DataTable dataTable = QueryDac.GetTable(sql);
                if (dataTable.Rows.Count > 0)
                {
                    sortID = dataTable.Rows[0]["Code"].ToString();
                    if (string.IsNullOrEmpty(sortID))
                    {
                        sortID = "1";
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine();
            }
            return sortID;
        }


        #endregion

        #region 保存

        protected void BtnSubmitClick(object sender, EventArgs eventArgs)
        {
            var dac = new DictionaryTypeDac();
            var model = new DictionaryTypeModel();
            var baseDac = new BaseDac<DictionaryTypeModel>();
            //添加
            if (!EditPage)
            {
                model = new DictionaryTypeModel();
                model.Name = txtCategoryName.Text.Trim();
                model.SortID = txtSortID.SortValue != 0 ? Convert.ToInt32(txtSortID.SortValue)+1 : 0;
                model.Code = GetSortID(); 
                model.Type = 8;
                model.Comment = "房型设施";
                model.IsDeleted = 0;
                //加载添加基本数据
                baseDac.LoadAddBaseModel(model);
                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(ID);
                model.Name = txtCategoryName.Text.Trim();
                model.SortID = txtSortID.SortValue != 0 ? txtSortID.SortValue : 0;
                //加载修改基本数据
                baseDac.LoadUpdateBaseModel(model);
                dac.Update(model);
            }

            FunHelper.Alert("保存成功", ListUrl());
        }
        #endregion
    }
}